package com.huika.dealontop.control.base.extend;

import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;

import com.huika.dealontop.R;
import com.huika.dealontop.control.base.extend.impl.IStatusPageProvider;
import com.huika.dealontop.support.http.RequestResult;
import com.huika.dealontop.utils.UIUtils;

/**
 * Author: Victor Wu
 * Email: victor-wu*foxmail.com
 * Time: 2015/11/12 14:04
 * Description: 可加载网络数据的Activity
 */
public abstract class BaseLoadableActivity<T> extends BaseActionBarActivity implements IStatusPageProvider<T> {

    private StatusPage<T> mStatusPage;

    private AnimationDrawable mLoadingAnimation;

    @Override
    public void setRootView() {
        mStatusPage = new StatusPage<>(UIUtils.getContext(), this);
//        mStatusPage.setPageProvider(this);
        setContentView(mStatusPage);

        onContentViewCreate();
    }

    /**
     * 加载和刷新数据过程中  是否显示预览界面 和 成功页面
     *
     * @return
     */
    public boolean isLoadingFloat() {
        return mStatusPage.isLoadingFloat();
    }

    /**
     * 设置加载和刷新数据过程中  是否显示预览界面 和 成功页面
     *
     * @param loadingFloat
     */
    public void setLoadingFloat(boolean loadingFloat) {
        this.mStatusPage.setLoadingFloat(loadingFloat);
    }

    /**
     * 当内容区成功创建后
     * 可以在这个方法里 执行数据加载 、初始化标题头、或初始化Intent的数据
     */
    protected abstract void onContentViewCreate();

    protected void startLoadData() {
        this.mStatusPage.startLoadData();
    }

    protected void cancelRequest() {
        this.mStatusPage.cancelRequest();
    }

    @Override
    public View onCreatePreview() {
        return null;
    }

    @Override
    public View onCreateLoadingView() {
        View loadingView = View.inflate(UIUtils.getContext(), R.layout.common_load_view, null);
        ImageView loadingImage = (ImageView) loadingView.findViewById(R.id.loadingImageView);
        //loadingImage.setBackgroundResource(R.drawable.loading_anim);
        mLoadingAnimation = (AnimationDrawable) loadingImage.getBackground();
        return loadingView;
    }

    @Override
    public View onCreateEmptyView() {
        View emptyView = View.inflate(UIUtils.getContext(), R.layout.common_empty_view, null);
        emptyView.findViewById(R.id.click_reload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startLoadData();
            }
        });
        return emptyView;
    }

    @Override
    public View onCreateErrorView() {
        View errorView = View.inflate(UIUtils.getContext(), R.layout.common_load_error, null);
        errorView.findViewById(R.id.click_reload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startLoadData();
            }
        });
        errorView.findViewById(R.id.click_checknetwork).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //打开设置页面
                // TODO
            }
        });
        return errorView;
    }

    @Override
    public void onPagerStateChanged(int oldState, int newState) {
        if (mLoadingAnimation != null) {
            if (newState == StatusPage.STATE_LOADING) {
                mLoadingAnimation.start();
            } else {
                mLoadingAnimation.stop();
            }
        }
    }

    @Override
    public StatusPage.StatusResult onCheckResult(RequestResult<T> requestResult) {

        if (requestResult == null || requestResult.getRs() == null) {
            return StatusPage.StatusResult.EMPTY;
        }
        /**请求状态, 0失败，1成功*/
        if (requestResult.flag == 1) {
            return StatusPage.StatusResult.SUCCESS;
        }

        return StatusPage.StatusResult.ERROR;
    }
}
